﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

using LdtDataEngin.ModuleDetail.BaseModule.Optimization;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Data.DataParseStorage;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;

namespace LdtDataEngin.ModuleDetail.OptimizationModule.ReasonElementOpt
{
    public class ReasonOptimization:OptimizationBase
    {
        public ReasonOptimization(DataContext context,string moudleName)
        {
            dataContext = context;
            base.ModuleName=moudleName;
        }

        public void Optimization()
        {
            Stopwatch stopwatch = new Stopwatch();
            SetModuleProcess(ModuleName, 0,dataContext.InteractiveShow.inputParameterModel);
            stopwatch.Start();
            DAL_An_anjiancunchu anjiancunchudal = new DAL_An_anjiancunchu();
            //优选结果数据
            List<DataModel> dataModelsOPList = new List<DataModel>();
            var anjiancunchuList = anjiancunchudal.GetList(dataContext.DataLayerOpt.CaseId, "3");
            foreach (var item in anjiancunchuList)
            {
                DataSoursModel dataSoursModel = GetDataSoursModel(ReadDataXML(item.Wenjianid));
                dataModelsOPList.AddRange(dataSoursModel.dataModelList.Where(x=>x.State!="2").Distinct().ToList());
            }

            ReasonElementOptimization(dataContext.DataLayerOpt.SourdatModleList);
            stopwatch.Stop();
            SetModuleProcess(ModuleName + "_" + stopwatch.Elapsed.TotalMilliseconds.ToString("0"), 100,dataContext.InteractiveShow.inputParameterModel);
        }


        /// <summary>
        /// 保存理由要素
        /// </summary>
        /// <param name="dataModelList">优选结果数据</param>
        public void ReasonElementOptimization(List<DataModel> dataModelList)
        {
            try
            {
                if (dataContext.DataLayerAns.ReasonDataModel != null && dataContext.DataLayerAns.ReasonDataModel.Any())
                {
                    XmlUtilCommon xmlUtilCommon = new XmlUtilCommon() { dataContext = dataContext };
                    //dataLayerStores.ReasonXML = xmlUtilCommon.DataModelByXml(dataLayerStores.ReasonDataModel, "");
                    var locationModel = dataContext.DataLayerSys.locationModelList.FirstOrDefault(x => x.DocType.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower());
                    string uid = CaseNodeUpdate(dataModelList, locationModel.ReasoneELementPath);
                    if (!string.IsNullOrEmpty(uid))
                    {
                        DelectNodeByDataModel(uid, dataModelList);
                        DataModel dataModelRoot = dataContext.DataLayerAns.ReasonDataModel.FirstOrDefault(a => a.ParentId == "" || a.ParentId == null);
                        if (dataModelRoot != null)
                        {
                            List<DataModel> dataModelListR = dataContext.DataLayerAns.ReasonDataModel.Where(a => a.ParentId == dataModelRoot.ID).ToList();
                            foreach (DataModel model in dataModelListR)
                            {
                                model.ParentId = uid;
                            }
                            dataModelList.AddRange(dataContext.DataLayerAns.ReasonDataModel);
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                dataContext.InteractiveShow.LogShow("理由要素填写失败！！" + ex.ToString());
            }

        }
    }
}
